package org.litespring.beans.factory.support;

import org.litespring.beans.factory.BeanFactory;
import org.litespring.beans.factory.config.RuntimeBeanReference;
import org.litespring.beans.factory.config.TypedStringValue;

public class BeanDefinitionValueResolver {

    private final BeanFactory beanFactory;

    public BeanDefinitionValueResolver(DefaultBeanFactory factory) {
        this.beanFactory = factory;
    }


    public Object resolveValueIfNecessary(Object value) {
        if (value instanceof RuntimeBeanReference) {
            RuntimeBeanReference reference = (RuntimeBeanReference)value;
            String beanName = reference.getBeanName();
            Object bean = this.beanFactory.getBean(beanName);
            return bean;
        }else if(value instanceof TypedStringValue){
            TypedStringValue stringValue = (TypedStringValue)value;
            return stringValue.getValue();
        }else {
            throw new RuntimeException("the value "+ value + "has not implemented");
        }
    }
}
